package com.txl.dao;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.txl.domain.ContactGroupBean;
import com.txl.exception.DaoException;
import com.txl.utils.JdbcUtils;

public class ContactGroupDao {

	QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
	
	//查找分组
	public ContactGroupBean find(String groupid) {
		try {
			String sql = "select * from contactgroup where groupid=?";
			ContactGroupBean c = (ContactGroupBean) qr.query(sql, groupid, new BeanHandler(ContactGroupBean.class));
			
			sql = "select * from contactgroup where lft<? and rgt>? order by lft";
			Object[] params = {c.getLft(),c.getRgt()};
			List list = (List) qr.query(sql, new BeanListHandler(ContactGroupBean.class), params);
			c.setParents(list);
			return c;
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	//添加分组
	public void add(ContactGroupBean cg) {
		// TODO Auto-generated method stub
		try {
			String sql = "insert into contactgroup(groupid,groupname,lft,rgt,groupmemo) values(?,?,?,?,?)";
			Object[] params = {cg.getGroupid(),cg.getGroupname(),cg.getLft(),cg.getRgt(),cg.getGroupmemo()}; 
			qr.update(sql,params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	//更新子分组
	public void updateAfter(int rgt) {
		// TODO Auto-generated method stub
		try {
			String sql1 = "update contactgroup set lft=lft+2 where lft>=?";
			String sql2 = "update contactgroup set rgt=rgt+2 where rgt>=?";
			qr.update(sql1, rgt);
			qr.update(sql2, rgt);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//删除分组
	public void delete(String groupid) {
		// TODO Auto-generated method stub
		try {
			String sql = "select * from contactgroup where groupid=?";
			ContactGroupBean c = (ContactGroupBean) qr.query(sql, groupid, new BeanHandler(ContactGroupBean.class));
			
			String sql1 = "delete from contactgroup where groupid=?";
			qr.update(sql1, groupid);
			
			//删除子分组
			String sql2 = "delete from contactgroup where lft>=? and rgt<=?";
			Object[] params = {c.getLft(),c.getRgt()};
			qr.update(sql2, params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	//获取所有分组
	public List<ContactGroupBean> getAll() {
		try {
			String sql = "select child.groupid,child.groupname,count(child.groupid) depth from contactgroup parent,contactgroup child where child.lft>=parent.lft and child.rgt<=parent.rgt group by child.groupid order by child.lft";
			return (List<ContactGroupBean>) qr.query(sql, new BeanListHandler(ContactGroupBean.class));
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

	//修改分组
	public void update(ContactGroupBean cg) {
		// TODO Auto-generated method stub
		try {
			String sql = "update contactgroup set groupname=?,groupmemo=? where groupid=?";
			Object params[] = {cg.getGroupname(),cg.getGroupmemo(),cg.getGroupid()};
			qr.update(sql,params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}

}
